﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ThousandFunClient.model;
using ThousandFunClient.Util;
using System.Collections;
using System.IO.Ports;
using System.Timers;
using log4net;
using System.Globalization;
using System.Threading;
using System.Media;
using System.Runtime.InteropServices;

namespace ThousandFunClient
{
    public partial class Fm_Exit : Form
    {
        public Fm_Exit()
        {
            InitializeComponent();
            
        }

        private void btn_changekeyboard_Click(object sender, EventArgs e)
        {
            this.txt_password.Focus();
            System.Diagnostics.Process.Start("osk.exe"); 
        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_shutdowncomputer_Click(object sender, EventArgs e)
        {
            //if (Constants.MainPassword == txt_password.Text.Trim())
            //20121210客户要求退出软件并关闭电脑时，不需要输入密码。但要求把其他Windows的软件，比如Sinudyn软件，最好
            //其他所有软件都要求密码才能启动。因此屏蔽掉此功能。
            {
                if (formString.Equals("fm_ThounsandFunMain"))
                {
                    fm_ThounsandFunMain.shutdownport();
                }
                if (formString.Equals("fm_MainForm640x480"))
                {
                    fm_MainForm640x480.shutdownport();
                }
                Application.Exit();
                this.DoExitWin(EWX_SHUTDOWN);
            }
            //else
            {
           //     MessageBox.Show(this, "密码错误");
           //     txt_password.Focus();
            }
        }

  
        Fm_ThounsandFunMain fm_ThounsandFunMain ;
        Fm_MainForm640x480 fm_MainForm640x480;
        private static string formString ="";
        private void btn_shutdownapp_Click(object sender, EventArgs e)
        {

            if(Constants.MainPassword == txt_password.Text.Trim())
            {
                if (formString.Equals("fm_ThounsandFunMain"))
                {
                    fm_ThounsandFunMain.shutdownport();
                }
                if (formString.Equals("fm_MainForm640x480"))
                {
                    fm_MainForm640x480.shutdownport();
                }
                Application.Exit();
            }
            else
            {
                MessageBox.Show(this, "密码错误");
                txt_password.Focus();
            }

        }

      

        #region 关机用到的API代码

        [StructLayout(LayoutKind.Sequential, Pack = 1)]

        internal struct TokPriv1Luid
        {
            public int Count;

            public long Luid;

            public int Attr;
        }

        [DllImport("kernel32.dll", ExactSpelling = true)]

        internal static extern IntPtr GetCurrentProcess();

        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

        internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

        [DllImport("advapi32.dll", SetLastError = true)]

        internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

        internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,

            ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]

        internal static extern bool ExitWindowsEx(int flg, int rea);

        internal const int SE_PRIVILEGE_ENABLED = 0x00000002;

        internal const int TOKEN_QUERY = 0x00000008;

        internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;

        internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

        internal const int EWX_LOGOFF = 0x00000000;//注销

        internal const int EWX_SHUTDOWN = 0x00000001;//关机

        internal const int EWX_REBOOT = 0x00000002;//重新启动

        internal const int EWX_FORCE = 0x00000004;//强制注销

        internal const int EWX_POWEROFF = 0x00000008;//强制关机

        internal const int EWX_FORCEIFHUNG = 0x00000010;

        private void DoExitWin(int flg)
        {
            bool ok;

            TokPriv1Luid tp;

            IntPtr hproc = GetCurrentProcess();

            IntPtr htok = IntPtr.Zero;

            ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);

            tp.Count = 1;

            tp.Luid = 0;

            tp.Attr = SE_PRIVILEGE_ENABLED;

            ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);

            ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);

            ok = ExitWindowsEx(flg, 0);
        }

        #endregion

        private void Fm_Exit_Load(object sender, EventArgs e)
        {
            System.Type typeFm_ThounsandFunMain = typeof(Fm_ThounsandFunMain);
            System.Type typeFm_MainForm640x480 = typeof(Fm_MainForm640x480);
            if (this.Owner.GetType().Equals(typeFm_MainForm640x480))
            {
                fm_MainForm640x480 = (Fm_MainForm640x480)this.Owner;
                formString = "fm_MainForm640x480";
  
            }
            else
            {
                fm_ThounsandFunMain = (Fm_ThounsandFunMain)this.Owner;
                formString = "fm_ThounsandFunMain";
            }
            
        }

        private void txt_password_TextChanged(object sender, EventArgs e)
        {

        }

        private void lbl_about_Click(object sender, EventArgs e)
        {

        }

    }
}
